package com.workManager.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.workManager.exception.BusinessException;
import com.workManager.mapper.TdOrganizationMapper;
import com.workManager.pojo.TdOrganization;
import com.workManager.pojo.TdOrganizationExample;

import com.workManager.vo.OrganizationVo;


import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Thinkpad on 2017/1/12 0012.
 */
@Service
public class OrganizationServiceImpl implements OrganizationService{

    @Autowired
    TdOrganizationMapper organizationMapper;
    public List<TdOrganization> findOrganizationList() {

        return organizationMapper.selectByExample(null);
    }

    public List<TdOrganization> queryUserOrganization(String userId) {
        return organizationMapper.queryUserOrganization(Integer.parseInt(userId));
    }

   // @Override
    public Map<String,Object> add(TdOrganization organization){
        organization.setCreateDate(new Date());
        organization.setIsValid(true);
        organizationMapper.insert(organization);
        Map<String,Object> reltMap=new HashMap<String, Object>();
        reltMap.put("result","添加成功。");
        return reltMap;
    }

   // @Override
    public Map<String,Object> edit(Integer organizationId,Integer parentOrgId,String orgName,String description){
        TdOrganization organization=organizationMapper.selectByPrimaryKey(organizationId);
        if (organization==null){
            throw new BusinessException("无效的部门id："+organizationId);
        }

        organization.setParentOrgId(parentOrgId);
        organization.setOrgName(orgName);
        organization.setDescription(description);
        organizationMapper.updateByPrimaryKeySelective(organization);

        Map<String,Object> reltMap=new HashMap<String, Object>();
        reltMap.put("result","添加成功。");
        return reltMap;
    }

   // @Override
    public Map<String,Object> del(Integer[] organizationIds){
        for (Integer orgId:organizationIds){
            TdOrganization organization=organizationMapper.selectByPrimaryKey(orgId);
            if (organization==null)
                continue;

            //删除本部门
            organizationMapper.deleteByPrimaryKey(orgId);

            //删除子部门
            TdOrganizationExample example=new TdOrganizationExample();
            example.createCriteria().andParentOrgIdEqualTo(orgId);
            List<TdOrganization> childOrgList=organizationMapper.selectByExample(example);

            if (childOrgList!=null){
                for (TdOrganization childOrg:childOrgList){
                    if (childOrg!=null&&childOrg.getOrganizationId()!=null){
                        Integer[] orgIds=new Integer[]{childOrg.getOrganizationId()};

                        //递归调用
                        del(orgIds);
                    }
                }
            }
        }

        Map<String,Object> reltMap=new HashMap<String, Object>();
        reltMap.put("result","删除成功。");
        return reltMap;
    }

  //  @Override
    public PageInfo<OrganizationVo> query(Map<String,Object> params)throws Exception{
        int pageno = !params.containsKey("pagenum")||params.get("pagenum")==null?1:Integer.parseInt(params.get("pagenum").toString());
        int pagesize = !params.containsKey("pagesize")||params.get("pagesize")==null?10:Integer.parseInt(params.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);

        List<OrganizationVo> orgList=organizationMapper.query(params);
        PageInfo<OrganizationVo> organizationPageInfo = new PageInfo<OrganizationVo>(orgList);

        /*TdOrganizationExample example=new TdOrganizationExample();
        if (params.get("orgName")!=null){
            example.createCriteria().andOrgNameLike("%"+(String)params.get("orgName")+"%");
        }
        List<TdOrganization> orgList=organizationMapper.selectByExample(example);
        List<OrganizationVo> voList=new ArrayList<OrganizationVo>();
        if (orgList!=null){
            total=((Page)orgList).getTotal();
            pages=((Page)orgList).getPages();

            for (TdOrganization organization:orgList){
                OrganizationVo vo=new OrganizationVo();
                PropertyUtils.copyProperties(vo,organization);

                if (vo.getParentOrgId()!=null){
                    TdOrganization parentOrg=organizationMapper.selectByPrimaryKey(vo.getParentOrgId());
                    if (parentOrg!=null)
                        vo.setParentOrgName(parentOrg.getOrgName());
                }
                voList.add(vo);
            }
        }*/


        return organizationPageInfo;
    }
}
